package com.hongxun.project.assets.iPole.controller;

import java.util.List;

import com.hongxun.project.assets.iPoint.domain.IPoint;
import com.hongxun.project.assets.iPoint.service.IIPointService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hongxun.framework.aspectj.lang.annotation.Log;
import com.hongxun.framework.aspectj.lang.enums.BusinessType;
import com.hongxun.project.assets.iPole.domain.IPole;
import com.hongxun.project.assets.iPole.service.IIPoleService;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.framework.web.page.TableDataInfo;
import com.hongxun.framework.web.domain.AjaxResult;
import com.hongxun.common.utils.poi.ExcelUtil;

/**
 * 低压杆塔 信息操作处理
 * 
 * @author hongxun
 * @date 2019-03-19
 */
@Controller
@RequestMapping("/assets/iPole")
public class IPoleController extends BaseController
{
    private String prefix = "assets/iPole";
	
	@Autowired
	private IIPoleService iPoleService;
	@Autowired
	private IIPointService iPointService;

	
	@RequiresPermissions("assets:iPole:view")
	@GetMapping()
	public String iPole()
	{
	    return prefix + "/iPole";
	}
	
	/**
	 * 查询低压杆塔列表
	 */
	@RequiresPermissions("assets:iPole:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(IPole iPole)
	{
		startPage();
        List<IPole> list = iPoleService.selectIPoleList(iPole);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出低压杆塔列表
	 */
	@RequiresPermissions("assets:iPole:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IPole iPole)
    {
    	List<IPole> list = iPoleService.selectIPoleList(iPole);
        ExcelUtil<IPole> util = new ExcelUtil<IPole>(IPole.class);
        return util.exportExcel(list, "iPole");
    }
	
	/**
	 * 新增低压杆塔
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存低压杆塔
	 */
	@RequiresPermissions("assets:iPole:add")
	@Log(title = "低压杆塔", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(IPole iPole)
	{		
		return toAjax(iPoleService.insertIPole(iPole));
	}

	/**
	 * 修改低压杆塔
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		IPole iPole1 = new IPole();
		iPole1.setPId(id);
		List<IPole> iPoleList = iPoleService.selectIPoleList(iPole1);
		IPole iPole = new IPole();
		if(iPoleList.size() == 0){
			IPoint pPoint = iPointService.selectIPointById(id);
			iPole1.setPId( pPoint.getPoleId());
			iPoleList = iPoleService.selectIPoleList(iPole1);
			iPole = iPoleService.selectIPoleById(iPoleList.get(0).getId());
		}else{
			iPole = iPoleService.selectIPoleById(iPoleList.get(0).getId());
		}
//		IPole iPole = iPoleService.selectIPoleById(id);
		mmap.put("iPole", iPole);
	    return prefix + "/edit";

	}
	
	/**
	 * 修改保存低压杆塔
	 */
	@RequiresPermissions("assets:iPole:edit")
	@Log(title = "低压杆塔", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(IPole iPole)
	{		
		return toAjax(iPoleService.updateIPole(iPole));
	}
	
	/**
	 * 删除低压杆塔
	 */
	@RequiresPermissions("assets:iPole:remove")
	@Log(title = "低压杆塔", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(iPoleService.deleteIPoleByIds(ids));
	}
	
}
